package com.micro.ai.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.auth.entity.AuditLog;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 操作审计日志Mapper
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Mapper
public interface AuditLogMapper extends BaseMapper<AuditLog> {

    /**
     * 分页查询审计日志
     */
    @Select("<script>" +
            "SELECT * FROM audit_logs " +
            "<where>" +
            "<if test='userId != null and userId != \"\"'> AND user_id = #{userId} </if>" +
            "<if test='tenantId != null and tenantId != \"\"'> AND tenant_id = #{tenantId} </if>" +
            "<if test='action != null and action != \"\"'> AND action = #{action} </if>" +
            "<if test='resourceType != null and resourceType != \"\"'> AND resource_type = #{resourceType} </if>" +
            "<if test='startTime != null'> AND created_at >= #{startTime} </if>" +
            "<if test='endTime != null'> AND created_at &lt;= #{endTime} </if>" +
            "</where>" +
            "ORDER BY created_at DESC" +
            "</script>")
    IPage<AuditLog> selectAuditLogPage(
            Page<AuditLog> page,
            @Param("userId") String userId,
            @Param("tenantId") String tenantId,
            @Param("action") String action,
            @Param("resourceType") String resourceType,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );

    /**
     * 查询用户的最近操作日志
     */
    @Select("SELECT * FROM audit_logs WHERE user_id = #{userId} ORDER BY created_at DESC LIMIT #{limit}")
    List<AuditLog> selectRecentLogsByUserId(@Param("userId") String userId, @Param("limit") Integer limit);

    /**
     * 查询资源的所有操作历史
     */
    @Select("SELECT * FROM audit_logs WHERE resource_type = #{resourceType} AND resource_id = #{resourceId} ORDER BY created_at DESC")
    List<AuditLog> selectLogsByResource(@Param("resourceType") String resourceType, @Param("resourceId") String resourceId);

    /**
     * 统计操作次数
     */
    @Select("<script>" +
            "SELECT action, resource_type, COUNT(*) as operation_count, MAX(created_at) as last_operation_time " +
            "FROM audit_logs " +
            "<where>" +
            "<if test='userId != null and userId != \"\"'> AND user_id = #{userId} </if>" +
            "<if test='tenantId != null and tenantId != \"\"'> AND tenant_id = #{tenantId} </if>" +
            "<if test='startTime != null'> AND created_at >= #{startTime} </if>" +
            "<if test='endTime != null'> AND created_at &lt;= #{endTime} </if>" +
            "</where>" +
            "GROUP BY action, resource_type ORDER BY operation_count DESC" +
            "</script>")
    List<AuditLog> selectOperationStats(
            @Param("userId") String userId,
            @Param("tenantId") String tenantId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );

    /**
     * 清理过期审计日志（物理删除）
     */
    @Delete("DELETE FROM audit_logs WHERE created_at < #{cutoffTime}")
    int deleteExpiredLogs(@Param("cutoffTime") LocalDateTime cutoffTime);
}
